package com.cg.train.service.Impl;

import com.cg.system.entity.PageBean;
import com.cg.train.entity.Train;
import com.cg.train.mapper.TrainMapper;
import com.cg.train.service.TrainService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TrainServiceImpl implements TrainService {
    @Autowired
    private TrainMapper trainMapper;
    @Override
    public void add(Train train) {
        trainMapper.add(train);
    }

    @Override
    public PageBean<Train> list(Integer pageNum, Integer pageSize, String projectCode, String trainingProject) {
//        创建PageBean对象
        PageBean<Train> pb = new PageBean<>();
        //开启分页查询PageHelper
        PageHelper.startPage(pageNum, pageSize);
//        调用mapper查询数据
        List<Train> list = trainMapper.list(projectCode, trainingProject);
        //Page中提供了方法，可以获取PageHelper分页查询后的总记录条数和当前页数据集合
        Page<Train> p = (Page<Train>) list;
//把数据填充到PageBean对象中
        pb.setTotal(p.getTotal());
        pb.setItems(p.getResult());
        return pb;
    }

    @Override
    public Train detail(Integer projectId) {
        return trainMapper.detail(projectId);
    }

    @Override
    public void delete(Integer id) {
        trainMapper.delete(id);
    }

    @Override
    public void update(Train train) {
        trainMapper.update(train);
    }
}
